function _formatNormalize(formatter) {
	if (typeof formatter === 'function') {
		return formatter;
	}
	if (typeof formatter !== 'string') {
		throw new Error('formatter must be a string or function');
	}
	if (formatter === 'date') {
		formatter = 'yyyy-MM-dd';
	} else if (formatter === 'datatime') {
		formatter = 'yyyy-MM-dd HH:mm:ss'
	}else if (formatter === 'datatimeFull') {
		formatter = 'yyyy-MM-dd HH:mm:ss:nn'
	}
	return (dateInfo) => {
		const {
			yyyy,
			MM,
			dd,
			HH,
			mm,
			ss,
			nn
		} = dateInfo;
		return formatter.replace(/yyyy/g, yyyy).replace(/MM/g, MM).replace(/dd/g, dd).replace(/HH/g, HH).replace(
			/mm/g, mm).replace(/ss/g, ss).replace(/nn/g, nn);
	}
}

function formate(date, formatter, isPad = false) {
	formatter = _formatNormalize(formatter);

	function _pad(value, length) {
		if (isPad) {
			return (value + '').padStart(length, '0');
		} else {
			return value;
		}
	}
	const dateInfo = {
		year: date.getFullYear(),
		month: date.getMonth() + 1,
		day: date.getDate(),
		hour: date.getHours(),
		minute: date.getMinutes(),
		second: date.getSeconds(),
		millisecond: date.getMilliseconds(),

	}
	dateInfo.yyyy = _pad(dateInfo.year, 4);
	dateInfo.MM = _pad(dateInfo.month, 2);
	dateInfo.dd = _pad(dateInfo.day, 2);
	dateInfo.HH = _pad(dateInfo.hour, 2);
	dateInfo.mm = _pad(dateInfo.minute, 2);
	dateInfo.ss = _pad(dateInfo.second, 2);
	dateInfo.nn = _pad(dateInfo.millisecond, 3);
	return formatter(dateInfo);
}
export  formate;
//自定义时间函数
// console.log(formate(new Date("2023-12-31"), (dateInfo) => {
// 	const {
// 		year
// 	} = dateInfo;
// 	const thisYear = new Date().getFullYear();
// 	if (year < thisYear) {
// 		return `${thisYear-year}年前`;
// 	} else if (year > thisYear) {
// 		return `${year-thisYear}年后`;
// 	} else {
// 		return "今年";
// 	}
// }));

// console.log(formate(new Date(), 'date', true));
// console.log(formate(new Date('2021-5-1'), 'date'));
// console.log(formate(new Date(),'datatime',true));
// console.log(formate(new Date(),'datatimeFull',true));